<template>
  <view>
    <!-- 实现吸顶效果 -->
    <view class="search-box">
      <!-- 搜索组件 -->
      <my-search @click="gotoSearch"></my-search>
    </view>
        <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="item in swiperData" :key="item.id">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?id='+item.id">
          <image :src="item.url" mode="widthFix"></image>
        </navigator>
      </swiper-item>
    </swiper>
  <!-- 导航栏数据 -->
  <view class="nav-list">
    <view class="nav-item" v-for="(item,index) in navList" :key="index" @click="clickTo(item)">
    <image :src="item.image_src" class="nav-img"></image>
    </view>
  </view>
  <!-- 楼层数据 -->
 <view class="floor-list">
   <view class="floor-item" v-for="(item,index) in floorList" :key="index">
     <image :src="item.floor_title.image_src" class="floor-title"></image>
     <!-- 楼层的图片区域 -->
     <view class="floor-img-box">
       <!-- 左侧大图片的盒子 -->
       <navigator class="left-img-box" :url='item.product_list[0].url'>
         <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
       </navigator>
       <!-- 右侧4个小图片的盒子 -->
       <view class="right-img-box">
         <!-- 由于这里要获取小图片,所以遍历product_list -->
         <!-- v-if="index1!==0" 把第一张图片去掉,因为第一张图片是左侧的大盒子里面的图片 -->
         <navigator :url='item1.url' class="right-img-item" v-for="(item1,index1) in item.product_list" :key="index1" v-if="index1!==0">
           <image :src="item1.image_src" :style="{width:item1.image_width+'rpx'}" mode="widthFix"></image>
         </navigator>
       </view>
     </view>
   </view>
 </view>
    </view>
</template>

<script>
  export default {
    data() {
      return {
        swiperData:[
          {id:1,url:'../../static/images/手机1.png'},
          {id:2,url:'../../static/images/手机2.png'},
          {id:3,url:'../../static/images/手机3.jpg'},
        ],
        navList:[],
        floorList:[]
      };
    },
    methods:{
      // 获取导航列表
      getNavList(){
       uni.request({
        url:'https://api-hmugo-web.itheima.net/api/public/v1/home/catitems',
        method:'GET',
        success:(res)=>{
          this.navList = res.data.message
        }
      })   
      },
      // 获取楼层数据
      getFloorList(){
        uni.request({
          url:'https://api-hmugo-web.itheima.net/api/public/v1/home/floordata',
          method:'GET',
          success:(res)=>{
            // 对数据进行处理
            res.data.message.forEach(floor=>{
              floor.product_list.forEach(prod=>{
                prod.url='/subpkg/goods_list/goods_list?'+prod.navigator_url.split('?')[1]
              })
            })
            console.log(res)
            this.floorList = res.data.message
          }
        })
      },
      // 点击进入分类页面
      clickTo(value){
       if(value.name=='分类'){
         uni.switchTab({
           url:'/pages/cate/cate'
         })
       }
      },
gotoSearch(){
  uni.navigateTo({
    url:'/subpkg/search/search'
  })
}
    },
    onLoad() {
      this.getNavList()
      this.getFloorList()
    }
  }
</script>

<style lang="scss">
  // 轮播图样式
swiper{
  height: 330rpx;
  .swiper-item{
    image{
      width: 100%;
      height: 100%;
    }
  }
}
// 导航栏样式
.nav-list{
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-img{
    width: 128rpx;
    height: 140rpx;
  }
}
// 楼层样式
.floor-title{
  height: 60rpx;
  width: 100%;
  display: flex;
}
.floor-img-box{
  display: flex;
  padding-left: 10rpx;
  .right-img-box{
    display: flex;
   flex-wrap: wrap;
   justify-content: space-around;
  }
}
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
